//
//  CZPictureAnimator.swift
//  weibo27
//
//  Created by mac on 16/4/24.
//  Copyright © 2016年 it.cast. All rights reserved.
//

import UIKit

class CZPictureAnimator: NSObject ,UIViewControllerTransitioningDelegate{

    private var isPresent: Bool = false
    
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresent = true
        
        return self
    }
    
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresent = false
        
        return self
    }
}


extension CZPictureAnimator:UIViewControllerAnimatedTransitioning{
    
    //返回动画的时长
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        
        return 1.0
    }
    
    //动画的具体内容，一旦实现这个方法，系统的就不会再提供系统的动画效果
    /**
    1. 容器视图 － 会将 Modal 要展现的视图包装在容器视图中
    存放的视图要显示－必须自己指定大小！不会通过自动布局填满屏幕
    2. viewControllerForKey: fromVC / toVC
    3. viewForKey: fromView / toView
    4. completeTransition: 无论转场是否被取消，都必须调用
    否则，系统不做其他事件处理！
    */
    
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        //容器视图控制器要指定大小
        let v = UIView(frame: UIScreen.mainScreen().bounds)
        v.backgroundColor = UIColor.redColor()
        
//        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
//        print(fromVC)
        
//        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
//        print(toVC)
        
//        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
//        print(fromView)
        
//        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
//        print(toView)
        
        
        isPresent ?  presentAnimation(transitionContext) : dismissedAnimation(transitionContext)
    }
    
    
    private func presentAnimation(transitionContext: UIViewControllerContextTransitioning){
        
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
        
        //进行转场
        transitionContext.containerView()?.addSubview(toView!)
        
        toView?.alpha = 0
        
        //开始动画
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            
            toView?.alpha = 1.0
            
            }) { (_) -> Void in
                
                //告诉系统转场动画完成
                transitionContext.completeTransition(true)
        }
    }
    
    private func dismissedAnimation(transitionContext: UIViewControllerContextTransitioning) {
        
        // 1. 获取要 dismiss 的控制器的视图
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        
        UIView.animateWithDuration(transitionDuration(transitionContext),
            animations: { () -> Void in
                
                fromView.alpha = 0
            }) { (_) -> Void in
                
                // 将 fromView 从父视图中删除
                fromView.removeFromSuperview()
                
                // 告诉系统动画完成
                transitionContext.completeTransition(true)
        }
    }
}



































